/**
 * 根据权限过滤路由
 */
import auth from "@/auth.js";

function perimission(route) {
    //admin
    let role = auth.getRole();
    //['admin','user'] includes方法用于判断当前数组是否包含传入参数
    return route.perimission.includes(role);
}

function filterRouter(routes) {
    let newRoutes = []
        //forEach用于遍历数组，e表示数组当中的元素
    routes.forEach((e) => {
        if (!perimission(e)) {
            return; //跳过当前循环
        }
        if (!e.hidden) { //hidden = false
            let children = []
            if (e.children.length > 0) {
                e.children.forEach(el => { //el指的是每个一个子路由
                        if (!perimission(el)) {
                            return;
                        }
                        if (!el.hidden) {
                            //路由访问地址拼接，
                            el.path = e.path + "/" + el.path
                            children.push(el) //把处理过的路由放入新的数组
                        }
                    })
                    //新的children需要装入到原来的根路由children属性
                e.children = children
            }
            newRoutes.push(e);

        }
    });
    return newRoutes
}

export { filterRouter }